﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseDll;
using Communicate;
using log4net;
using SerialDict;
using MotionIoLib;
using System.Threading;
using System.ComponentModel;
namespace OtherDevice
{
    [Description("高纳视觉")]

    public class AutoVisionControl:Vision_Base
    {
        public AutoVisionControl(string strName) : base(strName)
        {
            controlFrom = new Frm_TcpIp();
        }
        private static readonly object Lock = new object();
        public override int linkEtherNetControl()
        {
            if (link != null)
            {
                if (link.IsOpen())
                    link.Close();

            }
            link.RecvStringMessageEvent += (object sender, AsyTcpSocketEventArgs e) =>
            {
                if (e.Message != "EXWTRG")
                {
                    InMessageQueue(e.Message);
                }
            };
            link.SocketErrorEvent += (object sender, AsyTcpSocketEventArgs e) =>
            {
                return;
            };
            if (link.Open())
                return 0;
            else
                return -1;
        }

        #region 视觉算法调用
        public override int VisionControlUsing(string str_Meg, out string[] str_Result, Action actionMove = null, int nTimeout = 3000)
        {
            lock (Lock)
            {
                str_Result = null;
                clearMessageQueue();
                Send($"EXWTRG;{str_Meg}");
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
                while (true)
                {
                    Thread.Sleep(10);
                    string mes;
                    int ret = OutMessageQueue(out mes);

                    if (ret == 0)
                    {
                        //不需要注册函数，直接返回mes.X, mes.Y就可以
                        str_Result = mes.Split(',');
                        if (str_Result.Length == 0)
                            return -2;
                        if (str_Result[str_Result.Length - 1] == "0")
                            return -3;
                        return 0;
                    }
                    else if (stopwatch.ElapsedMilliseconds > nTimeout)
                    {
                        return -4;
                    }

                }

            }
        }
        public override bool VisionExposureChange(string str_Meg, int nTimeout = 3000)
        {
            lock (Lock)
            {
                clearMessageQueue();
                Send($"CSH,{str_Meg}");
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Restart();
                while (true)
                {
                    Thread.Sleep(10);
                    string mes;
                    int ret = OutMessageQueue(out mes);

                    if (ret == 0)
                    {
                        return true;
                    }
                    else if (stopwatch.ElapsedMilliseconds > nTimeout)
                    {
                        return false;
                    }

                }

            }
        }
    }

        #endregion



    
}